home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 646 / jukebx14 / src / mod_peek.c < prev   
C/C++ Source or Header  |  1992-05-13  |  7KB  |  399 lines

  1. #include <tos.h>
  2. #include <stdio.h>
  3.  
  4.  
  5. typedef enum {NO_FILE=-1, SEGMNT, INST31, INST15} IN_MEMORY;
  6.  
  7. typedef struct
  8. {
  9.     char name[22];   
  10.     unsigned short length;  /* offset to image of next instrument */
  11.     unsigned short volume;  
  12.     unsigned short transient_p;
  13.     unsigned short loop_p;
  14. } INST[32];
  15.  
  16.  
  17. typedef struct
  18. {
  19.     int instrument;
  20.     int frequency;
  21.     int command;
  22.     int parameter;
  23. } MODCOMMAND;
  24.  
  25.  
  26. typedef unsigned char SEQ[128];
  27. typedef unsigned long PATT[][64][4];
  28.  
  29. char *MusicBuffer = 0;
  30. int nr_sequence;
  31. SEQ *Seq;
  32. int nr_patt;
  33. PATT *Patt;
  34. INST *Inst;
  35.  
  36.  
  37. char *CommandName[] = 
  38. {
  39.     "Arpeggio","Porta Up","Porta Down","Tone Portamento",
  40.     "Vibrato","TonePlus Vol Slide","Vibrato Plus Vol Slide","*Tremolo",
  41.     "*8","*Sample Offset","Volume Slide","Song Jump",
  42.     "Set Volume","Patt. Break","*Extended Comm's*","Set Speed",
  43.  
  44.     "*Filter On Off","*Fine Porta Up","*Fine Porta Down","*Set Gliss Control",
  45.     "*Set Vibrato Control","*Set Fine Tune","*Jump Loop","*Set Tremolo Control",
  46.     "*8","*Retrig Note","*Volume Fine Up","*Volume Fine Down",
  47.     "*Note Cut","*Note Delay","*Pattern Delay","*Funk It"
  48. };
  49.     
  50.  
  51.  
  52. static void GetModulPar(char *buffer)
  53. /* This routine exports the global vars :
  54.  *   Seq, Patt, nr_patt,nr_sequnces
  55.  */
  56. {int i, a;
  57.  
  58.     Seq  = (SEQ *)(buffer + 0x3b8);
  59.     Patt = (PATT *)(buffer + 0x43c);    
  60.     Inst = (INST *)(buffer + 20);
  61.     nr_patt = 0;
  62.     nr_sequence = (int)buffer[0x3b6];
  63.     
  64.     for(i = 0; i < nr_sequence; ++i)
  65.     {
  66.         a = (*Seq)[i];
  67.         if (a > nr_patt) nr_patt = a;
  68.     }
  69. }
  70.  
  71.  
  72. static int readModule(int f, int file, long length) 
  73. {int i;
  74.  char *b;
  75.  
  76.     if (file == INST15)
  77.         length += 0x1E4;
  78.  
  79.     if (( MusicBuffer = Malloc(length) ) == 0)
  80.         return 1;
  81.  
  82.     b = MusicBuffer;
  83.  
  84.     if (file == INST15)
  85.     {
  86.         Fread(f, 20+15*30, b);
  87.         
  88.         b += 20+15*30;
  89.         
  90.         for(i=0; i<16*30; ++i) 
  91.             *b++ = 0;
  92.         
  93.         Fread(f, 128+2, b);
  94.         b += 128+2+4;
  95.         Fread(f, length, b);
  96.     }
  97.     else
  98.         Fread(f, length, b);
  99.  
  100.     b = MusicBuffer;
  101. /*    mt_fixup(b); */
  102.     GetModulPar(b);
  103.     
  104.     return 0;
  105. }
  106.  
  107.  
  108. static int getFileType(int f)
  109. {long info[2];
  110.  
  111.     Fread(f, 4, info);
  112.     
  113.     if (*info == 'SEGM')
  114.         return SEGMNT;
  115.     
  116.     Fseek(0x438, f, 0);
  117.     Fread(f, 4, info);
  118.     
  119.     if (*info == 'M.K.')
  120.         return INST31;
  121.     
  122.     return INST15;    
  123. }
  124.  
  125.  
  126. int loadModule(char *name)
  127. {int f, file, err;
  128.  long length;
  129.  
  130.     if ((f = Fopen(name,0) ) <= 0)
  131.         return -1;
  132.     
  133.      file = getFileType(f);
  134.      
  135.     length = Fseek(0, f, 2);
  136.     Fseek(0, f, 0);
  137.     err = 0;
  138.     
  139.     if (file == INST31 || file == INST15)
  140.         err = readModule(f, file, length);
  141.     
  142.     Fclose(f);
  143.     
  144.     return err;
  145. }
  146.  
  147.  
  148. void parseCommand(long encoded, MODCOMMAND *pcom)
  149. {
  150.  
  151.     pcom->frequency  = (int)( (encoded & 0x3ff0000L) >> 16 );
  152.     pcom->instrument = (int)( (encoded & 0xf000) >> 12 );
  153.     
  154.     if (encoded & 0x10000000L)
  155.         pcom->instrument += 16;
  156.  
  157.     pcom->parameter  = (int)( encoded & 0xff );
  158.     pcom->command    = (int)( (encoded & 0xf00) >> 8 );
  159.     
  160.     if (!pcom->parameter && !pcom->command)
  161.         pcom->command = -1;
  162.         
  163.     if (pcom->command == 0xE)
  164.     {
  165.         pcom->command = (pcom->parameter >> 4) + 16;
  166.         pcom->parameter &= 0xF;
  167.     }
  168. }
  169.  
  170.  
  171. void listSequence(void)
  172. {int i;
  173.  
  174.     printf("\nLIST SEQUENCE\n");
  175.  
  176.     for (i = 0; i < nr_sequence; i++)
  177.     {
  178.         if (i & 0xf)
  179.             printf(", ");
  180.         else
  181.             printf("\n%2d: ", i);
  182.             
  183.         printf("%2d",(int)(*Seq)[i]);
  184.     }
  185.     
  186.     printf("\n");
  187. }
  188.  
  189.  
  190.  
  191. void listPattern(int pattern,int voice)
  192. {int i;
  193.  MODCOMMAND pcom;
  194.  
  195.     printf("\nLIST PATTERN %d ---  Voice %d\n",pattern, voice);
  196.  
  197.     for (i = 0; i < 64; i++)
  198.     {
  199.         /* nothing in the command, skip it */
  200.         if ((*Patt)[pattern][i][voice] == 0)    
  201.             continue;
  202.  
  203.         printf("%2d: ",    i);
  204.         parseCommand((*Patt)[pattern][i][voice], &pcom);
  205.  
  206.         printf("inst:%3d --- freq:%3d --- ",
  207.             pcom.instrument,pcom.frequency);
  208.     
  209.         if (pcom.command >= 0)
  210.             printf("%16s(%3d)\n", CommandName[pcom.command],pcom.parameter);
  211.         else
  212.             printf("\n");
  213.     }
  214.     
  215.     printf("\n");
  216.     
  217. }
  218.  
  219.  
  220.  
  221. int InstrumentInfo(int i)
  222. {long leng,loop,tran;
  223.  long n_length,n_loopstart,n_replen;
  224.  int no_repeat,err;
  225.  
  226.     leng = (long)2*(*Inst)[i].length;
  227.     tran = (long)2*(*Inst)[i].transient_p;
  228.     loop = (long)2*(*Inst)[i].loop_p;
  229.  
  230.     printf("*** %2d: %22s ***\n", i, (*Inst)[i].name);
  231.     
  232.     printf("volume: %4d\n", (*Inst)[i].volume);
  233.     
  234.     printf("length: %4ld\n", leng);
  235.     printf("trans:  %4ld\n", tran);
  236.     printf("loop:   %4ld\n", loop);
  237.  
  238.     no_repeat = (loop == 2);
  239.     err = 0;
  240.     
  241.     if (tran)
  242.     {
  243.         n_loopstart = tran;
  244.         n_length    = loop+tran;
  245.         n_replen    = loop;
  246.         
  247.         err |= (n_length > leng);
  248.     }
  249.     else
  250.     {
  251.         n_loopstart = 0;
  252.         n_length    = leng;
  253.         n_replen    = loop;
  254.     }
  255.     
  256.     printf("(0 -> %ld, then ", n_length);
  257.     
  258.     if (no_repeat) 
  259.         printf("STOP)");
  260.     else
  261.     {
  262.         printf(" %ld -> %ld)",n_loopstart, n_replen);
  263.  
  264.         err |= (n_loopstart + n_replen > leng);
  265.     }
  266.     
  267.     if (err)
  268.         printf("-- Error in sample\n\n");
  269.     else
  270.         printf("\n\n");
  271.     
  272.     return err;
  273. }    
  274.  
  275.     
  276.     
  277.     
  278. void getResourceUse(void)
  279. {char       com[32], inst[32];
  280.  int        i, unsupported,inst_err;
  281.  long       waste;
  282.  MODCOMMAND pcom;
  283.  
  284.      /* clear "used-command" vector */
  285.      for(i = 0; i < 32; com[i++] = 0);
  286.      
  287.     /* clear "used-instrument" vector */ 
  288.      for (i = 0; i < 32; inst[i++] = 0);
  289.  
  290.     unsupported = 0;
  291.     inst_err = 0;
  292.     
  293.     for (i = 0; i < nr_patt*64*4; i++)
  294.     {
  295.         parseCommand((*Patt)[0][i>>2][i & 3], &pcom);
  296.         inst[pcom.instrument] = 1;
  297.         if (pcom.command >= 0)
  298.             com[pcom.command] = 1;
  299.     }
  300.     
  301.     
  302.     /*
  303.      *  List the used instruments 
  304.      */
  305.      
  306.     printf("\nUSED INSTRUMENTS :\n");
  307.     
  308.     for (i = 1; i < 32; i++)
  309.     {
  310.         if (inst[i])
  311.         {
  312.             inst_err += InstrumentInfo(i-1);
  313.         }
  314.     }
  315.     printf("\n");
  316.  
  317.     
  318.     /*
  319.      *  List the used commands 
  320.      */
  321.      
  322.     printf("USED COMMANDS :\n");
  323.     
  324.     for (i = 1; i < 32; i++)
  325.     {
  326.         if (com[i])
  327.         {
  328.             printf("%2d: %s\n", i, CommandName[i]);
  329.             if (*CommandName[i] == '*')
  330.                 unsupported++;
  331.         }
  332.     }
  333.     
  334.     printf("\n");
  335.     
  336.     if (unsupported)
  337.     {
  338.         printf("WARNING: %d ProTracker commands\n", unsupported);        
  339.     }
  340.     
  341.     if (inst_err)
  342.     {
  343.         printf("WARNING: %d errors in the samples\n", inst_err);        
  344.     }
  345.     
  346.     inst_err = 0;
  347.     waste    = 0;
  348.     
  349.     for (i = 1; i < 32; i++)
  350.     {
  351.         if (!inst[i] && (*Inst)[i-1].length > 0)
  352.         {
  353.             waste += (long)2*(*Inst)[i].length;
  354.             inst_err++;
  355.         }
  356.     }
  357.     
  358.     if (inst_err)
  359.     {
  360.         printf("WARNING: %d unused samples in MODule, using %ld bytes\n", inst_err, waste);        
  361.     }
  362.     
  363. }
  364.  
  365.  
  366. main(int argc, char *argv[])
  367. {int err,i;
  368.  
  369.     if (argc > 1)
  370.     {    
  371.         printf("File: %s\n\n",argv[1]);
  372.     
  373.         if ((err = loadModule(argv[1])) == 0)
  374.         {
  375.             printf("Name: %s\n\n",MusicBuffer);
  376.             
  377.             printf("Sequence Length: %d, Number of Patterns: %d, \n",
  378.                 nr_sequence, nr_patt);
  379.             
  380.             getResourceUse();
  381.             
  382.             listSequence();
  383.             for (i = 0; i < nr_patt; i++)
  384.             {
  385.                 listPattern(i,0);
  386.                 listPattern(i,1);
  387.                 listPattern(i,2);
  388.                 listPattern(i,3);
  389.             }
  390.         }
  391.         else
  392.             printf("Error by load : %d\n",err);
  393.             
  394.         if (MusicBuffer)
  395.             Mfree(MusicBuffer);
  396.     }
  397.     
  398.     return 0;
  399. }